//
//  FANRetweetView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/20.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit
import SnapKit
import YYText


class FANRetweetView: UIView {
    
    var retweetPicCons : Constraint?
    
    var dataViewModel:FANDataViewModel?{
    
        didSet{
            
//            retweetLabel.text = dataViewModel?.retweetText
            
            retweetLabel.attributedText = dataViewModel?.retweetAttributeString
            
            retweetPicCons?.uninstall()
            
            if let pic_urls = dataViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo((dataViewModel?.retweetPictureSize)!)
//                })
//                
//                pictureView.pic_urls = pic_urls
                
                pictureView.dataInfo = (pic_urls,dataViewModel!.retweetPictureSize)
                
                pictureView.hidden = false
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    retweetPicCons = make.bottom.equalTo(pictureView.snp_bottom).offset(FANMargin).constraint
                })
                
            }else{
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    retweetPicCons = make.bottom.equalTo(retweetLabel).offset(FANMargin).constraint
                })
                
            }
            
            
        }
    
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor = UIColor(white: 247/255, alpha: 1)
        
        addSubview(retweetLabel)
        
        addSubview(pictureView)
        
        retweetLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(FANMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(retweetLabel.snp_bottom).offset(FANMargin)
            
            make.leading.equalTo(retweetLabel.snp_leading)
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
           retweetPicCons = make.bottom.equalTo(pictureView.snp_bottom).offset(FANMargin).constraint
        }
        
    }
    
    private lazy var pictureView:FANPictureCollectionView = {
    
        let picView = FANPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        return picView
    
    }()
    
    private lazy var retweetLabel:YYLabel = {
    
        let label = YYLabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.preferredMaxLayoutWidth = ScreenW - 2 * FANMargin
        
        label.userInteractionEnabled = true
        
        return label
    
    }()
    
    

}
